package de.cms.kff.ctrl;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import de.cms.kff.dao.AutoDao;
import de.cms.kff.dao.DaoException;
import de.cms.kff.model.Auto;

@Controller
public class AutoController {
	private static Logger log = Logger.getLogger(AutoController.class);
	private AutoDao autoDao = null;

	public void setAutoDao(AutoDao autoDao) {
		this.autoDao = autoDao;
	}

	/**
	 * <p>
	 * Auto form request.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/EditAuto.html'.
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/EditAuto.html", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam(required = false) Integer id) {
		log.debug("EditAuto.html");
		ModelAndView mv = new ModelAndView();
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		if (id == null) {
			mv.addObject(new Auto());
		} else {
			mv.addObject(autoDao.findById(id));
		}
		mv.setViewName("edit-auto");
		return mv;
	}

	/**
	 * <p>
	 * Saves a auto.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/SaveAuto.html'.
	 * </p>
	 * 
	 * @return
	 */
	@RequestMapping(value = "/SaveAuto.html", method = RequestMethod.POST)
	public String save(Auto auto, Model model) {
		if (auto.getCreated() == null) {
			auto.setCreated(new Date());
		}
		Auto result = autoDao.save(auto);
		if (auto.getId() == null) {
			auto.setId(result.getId());
		}
		model.addAttribute("statusMessageKey", "mitarbeiter.form.msg.success");
		return "redirect:AlleAutos.html";
	}

	/**
	 * <p>
	 * Deletes a auto.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP POST and request '/LoescheAuto.html'.
	 * </p>
	 */
	@RequestMapping(value = "/LoescheAuto.html", method = RequestMethod.GET)
	public ModelAndView delete(Auto auto) {
		try {
			autoDao.delete(auto);
			return new ModelAndView("redirect:AlleAutos.html");
		} catch (DaoException e) {
			ModelAndView mv = new ModelAndView("error");
			mv.addObject("message", e.getMessage());
			return mv;
		}
	}

	/**
	 * <p>
	 * Searches for all autos and returns them in a <code>Collection</code>.
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/AlleAutos.html'.
	 * </p>
	 */
	@RequestMapping(value = "/AlleAutos.html", method = RequestMethod.GET)
	public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		mv.addObject("title", "Auto");
		mv.addObject("message", "Folgende Autos sind im Fuhrpark vorhanden");
		mv.addObject("autos", autoDao.findAll());
		mv.addObject("edit", true);
		mv.setViewName("list-autos");
		return mv;
	}

	/**
	 * <p>
	 * Searches for all free autos and returns them in a <code>Collection</code>
	 * .
	 * </p>
	 * 
	 * <p>
	 * Expected HTTP GET and request '/FreieAutos.html'.
	 * </p>
	 */
	@RequestMapping(value = "/FreieAutos.html", method = RequestMethod.GET)
	public ModelAndView freieAutos() {
		ModelAndView mv = new ModelAndView();
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		mv.addObject("title", "Verleih beginnen");
		mv.addObject(
				"message",
				"Folgende Autos stehen für den Verleih zur Auswahl. Bitte wählen Sie eine Möglichkeit aus:");
		mv.addObject("autos", autoDao.findFreie());
		mv.addObject("edit", false);
		mv.setViewName("list-autos");
		return mv;
	}

}
